﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyClass.MyEvent
{
    /// <summary>
    /// 事件类
    /// </summary>
    class MyEventArgs : EventArgs
    {
        private char keyChar;
        public MyEventArgs(char keyChar)
        { 
            this.keyChar = keyChar;
        }
        public char KeyChar
        {
            get { return keyChar; }
        }
    }

    class UserInputMonitor
    {
        public delegate void UserRequest(object sender, MyEventArgs e); //定义委托 
        public event UserRequest OnUserRequest; //此委托类型类型的事件
        public void Run()
        {
            bool finished = false;
            do
            {
                string inputString = Console.ReadLine();
                if (inputString != "")
                    OnUserRequest(this, new MyEventArgs(inputString[0]));
            } while (!finished);
        }
    }

    public class Client
    {
        Client(UserInputMonitor m)
        {
            m.OnUserRequest += new UserInputMonitor.UserRequest(this.ShowMessage);
            //m.OnUserRequest+=new m.UserRequest(this.ShowMessage);  //注意这种写法是错误的，因为委托是静态的
        }
        public static void Main()
        {
            UserInputMonitor monitor = new UserInputMonitor(); 
            new Client(monitor);
            monitor.Run();
        }
        private void ShowMessage(object sender, MyEventArgs e)
        {
            Console.WriteLine("捕捉到：{0}", e.KeyChar); 
        }
     
    }


}
